package com.toggery.POS.controller;

import com.toggery.POS.entity.Clothing;
import com.toggery.POS.entity.Inventory;
import com.toggery.POS.service.ClothingService;
import com.toggery.POS.service.InventoryService;
import com.toggery.POS.tool.DateTool;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by LJH on 2018/3/18
 */
@RestController
//默认返回json格式
@RequestMapping("/inventory")
@Api(tags = "进出库数据")
public class InventoryController {
    @Autowired
    InventoryService service;
    @Autowired
    ClothingService clothingService;

    @GetMapping("/list1")
    public Map findAllRecord(){
        Map map = new HashMap() ;
        map.put("data",service.findAllRecord());
        return map;
    }

    @GetMapping("/allList")
    public Map findAllInventory(){
        Map map = new HashMap() ;
        map.put("data",service.findAll());
        return map;
    }
    @GetMapping("/list/{id}")
    public Map findAllWithClothingId(@PathVariable int id){
        Map map = new HashMap() ;
        map.put("data",service.findAllWithClothing(id));
        return map;
    }


    @GetMapping("/deleteById/{id}")
    public Map deleteById(int id){
        Map map = new HashMap();
        if(service.deleteByid(id)){
            map.put("result",true);
            map.put("msg","删除成功");
        }else {
            map.put("result",false);
            map.put("msg","删除失败");
        }
        return map;
    }

    @PostMapping("/update")
    public Map updateInventory(Inventory inventory){
        HashMap map = new HashMap();
        if(service.updateInventory(inventory)){
            map.put("result",true);
            map.put("result","更新成功");
        }else {
            map.put("result",false);
            map.put("result","更新失败");
        }
        return map;
    }

    @PostMapping("/save")
    public Map saveInventory(@RequestBody(required=false) Map map1){
        Inventory inventory = new Inventory();
        Clothing clothing = clothingService.findById(Integer.parseInt(map1.get("id").toString()));
        inventory.setClothingid(clothing.getId());
        inventory.setClothingname(clothing.getClothingname());
        inventory.setNumber(Integer.parseInt(map1.get("number").toString()));
        inventory.setType(Integer.parseInt(map1.get("type").toString()));
        inventory.setCreattime(DateTool.getNow());
        HashMap map = new HashMap();
        if(service.save(inventory)){
            map.put("result",true);
            map.put("result","保存成功");
        }else {
            map.put("result",false);
            map.put("result","保存失败");
        }
        return map;
    }

}
